/*
	网站页面控制器
*/
'use strict';
var moment		= require('moment');
var fs 				= require('fs');
var AV 				= require('leanengine');
var mdCommon	= require('../modules/common');

var avAlbum 			= AV.Object.extend("Album");
var avComment 		= AV.Object.extend("Comment");
var avSysConfig 	= AV.Object.extend("SysConfig");
var avNews 				= AV.Object.extend("New");
var avTrip 				= AV.Object.extend("Trip");
var avTripNote 		= AV.Object.extend("TripNote");
var avImage 			= AV.Object.extend("Image");


/**
 * 我的首页
 * 2016.05.16  by 胡斌
 */
exports.myIndexPage = function(req,res) {
	return res.redirect('/my/photostream');
}

/**
 * 我的首页
 * 2016.05.16  by 胡斌
 */
exports.changeImagePage = function(req,res) {
	var queryImage 	= new AV.Query(avImage);
	queryImage.doesNotExist('searchname');
	queryImage.exists('name');
	queryImage.find().then(function(results){
		for(var i=0;i<results.length;i++){
			var iname=results[i].get("name");
			console.log(iname);
			results[i].set("searchname",iname.toLowerCase());
		}
		return AV.Object.saveAll(results);
	}).then(function(results){
		res.send("success");
	},function(error){
		res.send(error.message);
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.myUploadPage = function(req,res) {
	var querySysConfig 	= new AV.Query(avSysConfig);

	querySysConfig.equalTo("type","tag");
	querySysConfig.find().then(function(results){
		var tag={};
		for(var i=0;i<results.length;i++){
			var val=results[i].get("value");
			tag[results[i].get("name")]=val.split(",");
		}
		//console.dir(tag);
		res.render('main/my_upload.html',{
			user:req.AV.user,
			tag:tag
		});
	},function(error){
		next();
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doUpload = function(req,res) {
	//console.dir(req.query);
	//console.log(req.busboy);
	if(req.busboy){
		var base64data = [];
    var pubFileName = '';
    var pubMimeType = '';
    req.busboy.on('file',(fieldname, file, fileName, encoding, mimeType) => {
      var buffer = '';
      pubFileName = fileName;
      pubMimeType = mimeType;
			if(pubMimeType.indexOf('image/')>-1){
				file.setEncoding('base64');
	      file.on('data', function(data) {
	        buffer += data;
	      }).on('end', function() {
	        base64data.push(buffer);
	      });
			}
    }).on('finish', function() {
			if(pubMimeType.indexOf('image/')>-1){
      	var f = new AV.File(pubFileName, {base64: base64data[0]});
				try {
	        f.save().then(function(fileObj) {
						var image=new avImage();
						var iname=req.query.airline+"_"+req.query.acType
						+"_"+req.query.classes;
						image.set("file",fileObj);
						image.set("user",req.AV.user);
						image.set("isSys",false);
						image.set("airline",req.query.airline);
						image.set("acType",req.query.acType);
						image.set("classes",req.query.classes);
						image.set("flightNo",req.query.flightNo);
						image.set("from",req.query.from);
						image.set("to",req.query.to);
						image.set("name",iname);
						image.set("searchname",iname.toLowerCase());
						return image.save();
	        }).then(function(image){
						res.json({success:true,index:req.query.index,id:image.id});
					}, function(error) {
						console.error(error.message);
						res.json({success:false,error:error.message});
					});
	      }catch (err) {
	        console.error('uploadFile - ' + err);
	        res.status(502);
	      }
			}else{
				res.json({success:false,error:"Bad file format!"});
			}
    })
    req.pipe(req.busboy);
	}else{
    console.error('uploadFile - busboy undefined.');
    res.status(502);
  }
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.myImageListPage = function(req,res) {
	var uid					= req.AV.user.id;
	var pager,where = " user = pointer('_User','"+uid+"') and isSys=false";
	AV.Query.doCloudQuery('select count(*) from Image where'+where).then(function(data){
		//console.dir(data);
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select include user,* from Image where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize+" order by createdAt desc");
	}).then(function(data){
		if(req.body.ajax){
			res.json({results:data.results,pager:pager});
		}else{
			res.render('main/my_photostream.html',{
				user:req.AV.user,
				results:data.results,
				moment:moment,
				pager:pager
			});
		}
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.myImageDetailPage = function(req,res) {
	var tag={};
	var querySysConfig 	= new AV.Query(avSysConfig);
	var queryImage 			= new AV.Query(avImage);
	var imgs						= req.query.ids;

	querySysConfig.equalTo("type","tag");
	querySysConfig.find().then(function(results){
		for(var i=0;i<results.length;i++){
			tag[results[i].get("name")]=results[i].get("value").split(",");
		}
		queryImage.containedIn("objectId",imgs.split(","));
		return queryImage.first();
	}).then(function(result){
		//console.dir(tag);
		res.render('main/my_image_detail.html',{
			user:req.AV.user,
			imgs:imgs,
			album:req.query.album,
			result:result,
			tag:tag,
			moment:moment
		});
	},function(error) {
		if(error==404){
			next();
		}else{
			mdCommon.sentErr(res,req,error,"main");
		}
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doEditImage = function(req,res) {
	var queryImage 			= new AV.Query(avImage);
	var imgs						= req.body.ids;
	var user						= req.AV.user;
	//console.log(imgs);
	queryImage.containedIn("objectId",imgs.split(","));
	queryImage.find().then(function(results){
		var promises = [];
		for(var i=0;i<results.length;i++){
			if(results[i].get("user") && results[i].get("user").id==user.id){
				results[i].set("airline",req.body.airline);
				results[i].set("acType",req.body.acType);
				results[i].set("classes",req.body.classes);
				results[i].set("flightNo",req.body.flightNo);
				results[i].set("from",req.body.from);
				results[i].set("to",req.body.to);
				promises.push(results[i].save());
			}
		}

		return AV.Promise.when(promises);
	}).then(function(result){
		if(req.body.album){
			res.redirect('/my/album/detail?id='+req.body.album);
		}else{
			res.redirect('/my/photostream');
		}
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doDeleteImage = function(req,res) {
	var queryImage 			= new AV.Query(avImage);
	var imgs						= req.query.ids;
	var user						= req.AV.user;

	//console.log(imgs);
	queryImage.containedIn("objectId",imgs.split(","));
	queryImage.find().then(function(results){
		var promises = [];
		for(var i=0;i<results.length;i++){
			if(user.get("adminRole")=="admin" || (results[i].get("user") && results[i].get("user").id==user.id)){
				promises.push(results[i].destroy());
			}
		}

		return AV.Promise.when(promises);
	}).then(function(result){
		if(req.body.ajax){
			res.json({success:true});
		}else if(req.query.album){
			res.redirect('/my/album/detail?id='+req.query.album);
		}else{
			res.redirect('/my/photostream');
		}
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doRemoveImage = function(req,res) {
	var album;
	var queryImage 			= new AV.Query(avImage);
	var imgs						= req.query.ids;
	var id							= req.query.album;
	var user						= req.AV.user;

	var queryAlbum 	= new AV.Query(avAlbum);
	if(!id)id="-1";

	queryAlbum.equalTo("objectId",id);
	queryAlbum.first().then(function(result) {
		if(typeof(result)!="undefined"){
			album=result;
			if(album.get("user") && album.get("user").id==user.id){
				queryImage.containedIn("objectId",imgs.split(","));
				return queryImage.find();
			}else{
				return AV.Promise.error("Permission denied！");
			}
		}else{
			return AV.Promise.error(404);
		}
	}).then(function(results){
		var relation 	= album.relation("images");
		for(var i=0;i<results.length;i++){
			relation.remove(results[i]);
		}
		return album.save();
	}).then(function(result){
		res.redirect('/my/album/detail?id='+result.id);
	},function(error) {
		if(error==404){
			next();
		}else{
			mdCommon.sentErr(res,req,error,"main");
		}
	});
}


/**
 * 我的相册列表
 * 2016.05.16  by 胡斌
 */
exports.myAlbumListPage = function(req,res) {
	var uid					= req.AV.user.id;
	var pager,where	= " user = pointer('_User','"+uid+"')";
	AV.Query.doCloudQuery('select count(*) from Album where'+where).then(function(data){
		console.dir(data);
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select * from Album where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize+" order by createdAt desc");
	}).then(function(data){
		if(req.body.ajax){
			res.json({results:data.results,pager:pager});
		}else{
			res.render('main/my_album.html',{
				user:req.AV.user,
				results:data.results,
				moment:moment,
				pager:pager,
				addimg:req.query.addimg,
				ids:req.query.ids
			});
		}
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的相册详情
 * 2016.05.16  by 胡斌
 */
exports.myAlbumDetailPage = function(req,res) {
	var album,pager,where=" enabled=true and isSys=false";
	var id					= req.query.id;
	var uid					= req.AV.user.id;
	var queryAlbum 	= new AV.Query(avAlbum);
	if(!id)id="-1";

	queryAlbum.equalTo("objectId",id);
	queryAlbum.first().then(function(result) {
		if(typeof(result)!="undefined"){
			album	= result;
			if(album.get("user") && album.get("user").id==uid){
				where += " and related images to pointer('Album', '"+result.id+"')";
				return AV.Query.doCloudQuery('select count(*) from Image where'+where);
			}else{
				return AV.Promise.error("Permission denied！");
			}
		}else{
			return AV.Promise.error(404);
		}
	}).then(function(data){
		//console.dir(data);
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select * from Image where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize+" order by createdAt desc");
	}).then(function(data){
		if(req.body.ajax){
			res.json({results:data.results,pager:pager});
		}else{
			res.render('main/my_album_detail.html',{
				user:req.AV.user,
				result:album,
				results:data.results,
				moment:moment,
				pager:pager
			});
		}
	},function(error) {
		if(error==404){
			next();
		}else{
			mdCommon.sentErr(res,req,error,"main");
		}
	});
}

/**
 * 我的相册详情
 * 2016.05.16  by 胡斌
 */
exports.doAddPhotos = function(req,res) {
	var queryAlbum 	= new AV.Query(avAlbum);
	var id					= req.query.id;
	if(!id)id="-1";
	queryAlbum.equalTo("objectId",id);
	queryAlbum.first().then(function(Album) {
		if(Album){
			var relation 	= Album.relation("images");
			var ids				= req.query.ids;
			var imgs			= ids.split(',');
			//console.log(imgs);
			for(var i=0;i<imgs.length;i++){
				var image		= new avImage();
				image.id		= imgs[i];
				relation.add(image);
			}
			return Album.save();
		}else{
			return AV.Promise.error('Album does not exist!');
		}
	}).then(function(Album){
		res.redirect('/my/album/detail?id='+Album.id);
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});

}

/**
 * 我的相册详情
 * 2016.05.16  by 胡斌
 */
exports.doCreateAlbum = function(req,res) {
	var newAlbum 	= new avAlbum();
	//console.dir(req.body.imgs);
	newAlbum.set("title",req.body.airline+"_"+req.body.acType
	+"_"+req.body.classes);
	newAlbum.set("detail","Flight No.:"+req.body.flightNo+" From:"+req.body.from+" To:"+req.body.to);
	newAlbum.set("user",req.AV.user);
	newAlbum.save().then(function(Album){
		var cover			= new avImage();
		var relation 	= Album.relation("images");
		var imgs			= req.body.imgs;
		for(var i=0;i<imgs.length;i++){
    	var image		= new avImage();
			image.id		= imgs[i];
      relation.add(image);
    }
		cover.id			= imgs[0];
		Album.set("cover",cover);
    return Album.save();
	}).then(function(Album){
		res.json({success:true,id:Album.id});
	},function(error) {
		res.json({success:false,error:error.message});
	});
}

/**
 * 我的相册列表
 * 2016.05.16  by 胡斌
 */
exports.myTripListPage = function(req,res) {
	var user				= req.AV.user;
	var pager,where	= " user = pointer('_User','"+user.id+"')";
	AV.Query.doCloudQuery('select count(*) from Trip where'+where).then(function(data){
		//console.dir(data);
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select * from Trip where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize+" order by createdAt desc");
	}).then(function(data){
		res.render('main/my_trip.html',{
			user:req.AV.user,
			results:data.results,
			moment:moment,
			pager:pager
		});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 创建游迹页面
 * 2016.08.30  by 胡斌
 */
exports.myCreateTripPage = function(req,res) {
	res.render('main/my_trip_create.html',{
		user:req.AV.user
	});
}

/**
 * 创建游迹
 * 2016.08.30  by 胡斌
 */
exports.doCreateTrip = function(req,res) {
	var query,images,trip;
	var user				= req.AV.user;
	var type				= req.body.type;
	var title 			= req.body.title;
	var detail 			= req.body.detail;
	var conclusion	= req.body.conclusion;
	var ids       	= req.body.ids;
	if(type=="all"){
		query = new AV.Query(avImage);
		query.containedIn("objectId",ids);
		query.select("objectId");
	}else{
		query = new AV.Query(avAlbum);
		query.equalTo("objectId",ids[0]);
	}
	query.equalTo("user",user);
	query.find().then(function(results){
		if(type=="all"){
			return AV.Promise.as(results);
		}else if(results.length>0){
			var relation = results[0].relation('images');
  		query = relation.query();
			query.select("objectId");
			return query.find();
		}
	}).then(function(results){
		images=results;
		if(images.length==0){
			AV.Promise.error("Can not find images！");
		}else{
			var newTrip = new avTrip();
			newTrip.set("user",user);
			newTrip.set("title",title);
			newTrip.set("detail",detail);
			newTrip.set("conclusion",conclusion);
			newTrip.set("user",user);
			newTrip.set("enabled",false);
			newTrip.set("isDraft",true);
			newTrip.set("cover",images[0]);
			return newTrip.save();
		}
	}).then(function(result){
		trip=result;
		var promises=[];
		for(var i=0;i<images.length;i++){
			var tripNote = new avTripNote();
			tripNote.set("sort",i);
			tripNote.set("image",images[i]);
			tripNote.set("trip",trip);
			promises.push(tripNote.save());
		}
		return AV.Promise.when(promises);
	}).then(function(results){
		var relation = trip.relation('tripNotes');
		for(var i=0;i<results.length;i++){
			relation.add(results[i]);
		}
		return trip.save();
	}).then(function(results){
		res.json({success:true,trip:trip.id});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});

}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doEditTrip = function(req,res) {
	var queryTrip 			= new AV.Query(avTrip);
	var id							= req.query.id;
	var user						= req.AV.user;
	if(!id)id="-1";
	//console.log(imgs);
	queryTrip.equalTo("objectId",id);
	queryTrip.first().then(function(result){
		if(result){
			result.set("enabled",true);
			result.set("isDraft",false);
			return result.save();
		}else{
			return AV.Promise.error("Can't find this trip!");
		}
	}).then(function(result){
		res.json({success:true});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doDeleteTrip = function(req,res) {
	var queryTrip 			= new AV.Query(avTrip);
	var id							= req.query.id;
	var user						= req.AV.user;
	if(!id)id="-1";

	//console.log(imgs);
	queryTrip.equalTo("objectId",id);
	queryTrip.first().then(function(result){
		if(!result){
			return AV.Promise.error("Can't find this trip！");
		}else if(user.get("adminRole")=="admin" || (result.get("user") && result.get("user").id==user.id)){
			return result.destroy();
		}else{
			return AV.Promise.error("Permission denied！");
		}
	}).then(function(result){
		//console.log(result);
		res.json({success:true});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doEditTripNote = function(req,res) {
	var queryTripNote 	= new AV.Query(avTripNote);
	var id							= req.body.id;
	if(!id)id="-1";
	//console.log(imgs);
	queryTripNote.equalTo("objectId",id);
	queryTripNote.first().then(function(TripNote){
		if(!TripNote){
			return AV.Promise.error("Can't find the TripNote！");
		}else{
			TripNote.set("text",req.body.text);
			TripNote.set("sort",parseInt(req.body.sort));
			return TripNote.save();
		}
	}).then(function(result){
		res.json({TripNote:result.id});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.doDeleteTripNote = function(req,res) {
	var queryTripNote 	= new AV.Query(avTripNote);
	var ids							= req.query.ids;
	var user						= req.AV.user;

	//console.log(imgs);
	queryTripNote.containedIn("objectId",ids);
	queryTripNote.include("trip");
	queryTripNote.find().then(function(results){
		var trip;
		if(results.length>0)trip = results[0].get("trip");
		if(user.get("adminRole")=="admin" || (trip && trip.get("user") && trip.get("user").id==user.id)){
			return AV.Object.destroyAll(results);
		}else{
			return AV.Promise.error("Permission denied！");
		}
	}).then(function(result){
		//console.log(result);
		res.json({success:true});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 我的图片列表
 * 2016.05.16  by 胡斌
 */
exports.myFavoriteListPage = function(req,res) {
	var uid					= req.AV.user.id;
	var pager,where = " objectId is exists";
	var type				= req.query.type?req.query.type:"image";
	var className		= type=="image"?"Image":"Trip";
	var key					= type=="image"?"likeImages":"likeTrips";

	//console.dir(req.AV.user.get("likeImages"));
	where	+= " and related "+key+" to pointer('_User', '"+req.AV.user.id+"')";
	AV.Query.doCloudQuery('select count(*) from '+className+' where'+where).then(function(data){
		//console.dir(data);
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select include user,* from '+className+' where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize+" order by createdAt desc");
	}).then(function(data){
		var tpl='main/my_favorite_image.html';
		if(type!="image")tpl='main/my_favorite_trip.html';
		res.render(tpl,{
			user:req.AV.user,
			results:data.results,
			moment:moment,
			pager:pager
		});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

/**
 * 列表页面
 * 2015.06.02  by 胡斌
 */
exports.myCommentListPage = function(req, res) {
	var pager,where	= " objectId is exists";
	var type				= req.query.type?req.query.type:"made";
	var ajax				= req.query.ajax;
	var uid					= req.AV.user.id;
	//console.dir(req.query);
	//console.dir(req.body);
	if(type=="received"){
		where	+= " and fromUser =pointer('_User','"+uid+"')";
	}else{
		where	+= " and toUser =pointer('_User','"+uid+"') and enabled=true";
	}

	AV.Query.doCloudQuery('select count(*) from Comment where'+where).then(function(data){
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select include new,include trip,include image,include toUser,include fromUser,* from Comment where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize);
	}).then(function(data) {
		if(ajax){
			res.render('main/ajax-my-comment-list.html',{
				results:data.results,
				moment:moment,
				type:type
			});
		}else{
			//console.dir(data.results);
			res.render('main/my_comment.html',{
				user:req.AV.user,
				results:data.results,
				moment:moment,
				pager:pager,
				type:type
			});
		}
	},function(error) {
		if(ajax){
			res.render('main/ajax-my-comment-list.html',{
				results:[],
				moment:moment,
				type:type
			});
		}else{
			next();
		}
	});
}

exports.doComment = function(req, res) {
	var id					= req.body.id;
	var type				= req.body.type;
	var toUid				= req.body.to;
	var query;
	//console.log(id);
	if(type=="image"){
		query	= new AV.Query(avImage);
	}else if(type=="comment"){
		query	= new AV.Query(avComment);
	}else if(type=="trip"){
		query	= new AV.Query(avTrip);
	}else{
		query	= new AV.Query(avNew);
	}

	query.equalTo("objectId",id);
	query.first().then(function(result){
		if(result){
			//console.log(result);
			var toUser;
			var newComment	= new avComment();
			if(toUid){
				toUser	= new AV.User();
				toUser.id	= toUid;
			}else{
				toUser	= result.get("user");
			}
			newComment.set("content",req.body.content);
			newComment.set("toUser",toUser);
			newComment.set("fromUser",req.AV.user);
			newComment.set("type",type);
			newComment.set(type,result);
			return newComment.save();
		}else{
			return AV.Promise.error("Can not find "+type+"！");
		}
	}).then(function(comment){
		res.json({success:true});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

exports.doToggleFav = function(req, res) {
	var user		= req.AV.user;
	var id			= req.body.id;
	var type		= req.body.type;
	var act			= req.body.act;
	var query,relation,increment=1;
	if(type=="image"){
		query			= new AV.Query(avImage);
		relation	= user.relation("likeImages");
	}else{
		query			= new AV.Query(avTrip);
		relation	= user.relation("likeTrips");
	}

	query.equalTo("objectId",id);
	query.first().then(function(result){
		if(result){
			if(act=="add"){
				relation.add(result);
			}else{
				increment=-1;
				relation.remove(result);
			}
			result.increment('likes', increment);
			result.save();
			return AV.Promise.when(user.save(),result.save());
		}else{
			return AV.Promise.error("Can't find the "+type+"!");
		}
	}).then(function(user){
		res.json({success:true});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}

exports.doSetAvatar = function(req, res) {
	var user		= req.AV.user;
	var id			= req.body.image;
	var query		= new AV.Query(avImage);

	query.equalTo("objectId",id);
	query.first().then(function(result){
		if(result){
				user.set("avatar",result.get("file"));
		}
		return user.save();
	}).then(function(user){
		res.json({success:true});
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}


exports.doCheckFav = function(req, res) {
	var user		= req.AV.user;
	var id			= req.body.id;
	var type		= req.body.type;
	var object,relation;
	if(type=="image"){
		object		= new avImage();
		relation	= user.relation("likeImages");
	}else{
		object		= new avTrip();
		relation	= user.relation("likeTrips");
	}
	object.id;
	var query		= relation.query();
	query.equalTo("objectId",id);
	query.first().then(function(result){
		if(result){
			res.json({success:true});
		}else{
			res.json({success:false});
		}
	},function(error) {
		mdCommon.sentErr(res,req,error,"main");
	});
}


/**
 * 后台登录页面
 * 2015.04.18  by 胡斌
 */
exports.doCkAuth = function(req,res,next) {
	//console.log(req.originalUrl);
	if(!req.AV.user){// 判断用户是否已经登录
		if(req.query.ajax){
			res.json({success:false,error:"Authority expired!"});
		}else{
			res.redirect('/login?refer='+req.originalUrl);
			return false;
		}
	}else{
		next();
	}
}
